package javabasics.concurrent.masterworker;

import java.util.Map;
import java.util.Set;

/**
 * @Author suxiaoxiao
 * @Date 2019/6/16 11:36
 * @Version 1.0
 */
public class MasterWorkerTest {

    public static void main(String[] args) {
        int jobNum = 100;
        // 固定使用5个Worker,并指定Worker
        Master m = new Master(new PlusWorker(), 5);
        // 提交 100 个子任务
        for (int i = 0; i <= jobNum; i++) {
            // 开始计算
            m.submit(i);
        }
        m.execute();
        // 最终结果
        int re = 0;
        Map<String, Object> resultMap = m.getResultMap();

        while (resultMap.size() > 0 || !m.isComplete()) {
            Set<String> keys = resultMap.keySet();
            String key = null;
            for (String k : keys) {
                key = k;
                break;
            }
            Integer i = null;
            if (key != null) {
                i = (Integer) resultMap.get(key);
            }
            if (i != null) {
                re += i;
            }
            if (key != null) {
                resultMap.remove(key);
            }
        }
        System.out.println(re);
    }

}
